# Definition for a binary tree node.
class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution(object):
    def leafSimilar(self, root1, root2):
        """
        :type root1: TreeNode
        :type root2: TreeNode
        :rtype: bool
        """
        if (not root1 and not root2) or (not root1 and root2 ) or (root1 and not root2):
            return False
        a = []
        b = []
        self.find(root1,a)
        self.find(root2,b)
        if a==b:
            return True
        else:
            return False




    def find(self,root,m):
        if not root:
            return []
        if root:
            if not root.left and not root.right:
                m.append(root.val)
        return self.find(root.left,m) or self.find(root.right,m)
